package com.practice.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.practice.dto.ReadDeviceStateDTO;
import com.practice.dto.ReadRequestDTO;
import com.practice.dto.ReadResponseDTO;
import com.practice.dto.WriteRequestDTO;
import com.practice.response.ResponseR;
import com.practice.service.TestService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * desc
 *
 * @author: kayn
 * 2024-09-04 17:15
 **/
@Slf4j
@RestController
@RequestMapping(value = "/api/robot/request/device")
public class DoorController {

    @Resource
    private TestService testService;

    /**
     * 写
     * @return 1
     */
    @RequestMapping(value = "/write", method = RequestMethod.POST)
    public ResponseR<?> write(@RequestBody @Validated WriteRequestDTO param) {

        log.info("接收到write请求,请求体：{}", JSONObject.toJSONString(param));

        return ResponseR.ok();
    }


    /**
     * 读
     * @return 1
     */
    @RequestMapping(value = "/read", method = RequestMethod.POST)
    public ReadResponseDTO write(@RequestBody @Validated ReadRequestDTO param) {

        log.info("接收到read请求,请求体：{}", JSONObject.toJSONString(param));

        ReadResponseDTO resp = new ReadResponseDTO();

        ReadDeviceStateDTO readDeviceStateDTO = new ReadDeviceStateDTO();
        readDeviceStateDTO.setCode(param.getDevices().get(0).getCode());
        readDeviceStateDTO.setState("OPEN");

        resp.setDeviceStates(CollUtil.newArrayList(readDeviceStateDTO));
        resp.setSuccess(Boolean.TRUE);
        resp.setMessage("成功");

        return resp;
    }

    /**
     * Hello World
     * @return
     */
    @RequestMapping(value = "/helloWorld")
    public String helloWord() {
        return "Hello World";
    }


}
